import { createRouter, createWebHistory } from "vue-router";

const router = createRouter({
  history: createWebHistory(),
  routes: [
    {
      path: "/",
      redirect: "/films/nowPlaying",
      meta: { needCityId: true },
    },
    // {
    //   path: '/films',
    //   redirect: '/films/nowPlaying'
    // },
    {
      path: "/home",
      component: () => import("@/pages/home/index.vue"),
      redirect: "/films/nowPlaying",
      meta: { needCityId: true },
      children: [
        {
          path: "/films/:target",
          component: () => import("@/pages/home/children/filmsView.vue"),
          name: "films",
          meta: { needCityId: true },
        },
        {
          path: "/cinemas",
          component: () => import("@/pages/home/children/cinemasView.vue"),
          name: "cinemas",
          meta: { needCityId: true },
        },
        {
          path: "/center",
          component: () => import("@/pages/home/children/centerView.vue"),
          name: "center",
          meta: { needCityId: true },
        },
      ],
    },
    {
      path: "/film/:filmId",
      component: () => import("@/pages/filmdetail/index.vue"),
      name: "filmdetail",
      meta: { needCityId: true },
    },
    {
      path: "/film/:filmId/cinemas",
      component: () => import("@/pages/filmcinemas/index.vue"),
      name: "filmcinemas",
      meta: { needCityId: true },
    },
    {
      path: "/cinema/:cinemaId/film/:filmId?/:time?",
      component: () => import("@/pages/cinemaDetail/index.vue"),
      name: "cinemadetail",
      meta: { needCityId: true },
    },
    {
      path: "/city",
      component: () => import("@/pages/city/index.vue"),
      name: "city",
      meta: { needCityId: false },
    },
  ],
});

router.beforeEach((to, from) => {
  // console.log(to);
  // console.log(from);
  const token = localStorage.getItem("cityId");
  if (to.meta.needCityId) {
    if (!token) {
      return { path: "/city" };
    }
  }
});

export default router;
